<?php
/*
 * @version :  2013-3-18
 * @project :  account-system
 * @Author  :  Allen
 * @e-mail  :  shulei@maszy.cn
 * @des		:  找回密码
 */
/**
 * 调用配置文件，并连接数据库
 */
require_once (dirname(__FILE__)."/../../config/config.php");
/**
	根据method 调用 代码块
 */
$method = $_REQUEST['method'];
if(empty($_SESSION['lang'])){
    $_SESSION['lang']= "cn";
}elseif($_GET['lang'] == "en"){
    $_SESSION['lang']= "en";
}elseif($_GET['lang'] == "cn"){
      $_SESSION['lang']= "cn";
}

if($method == 'getPass'){
	/**
		获取页面提交参数
	 */
	$userName = $_REQUEST['userName'];
	$userEmail = $_REQUEST['userEmail'];
	/**
	 *	设置返回成功和失败的信息 
	 */
	$errorTxt.='';
	$successTxt.='';
	$enErrorTxt.='';
	/**
	 * 验证输入的账号和邮箱是否存在 返回各种错误
	 */
	if(!empty($userName)){
		if(!empty($userEmail)){
			$sql="select * from zy_user where user_name='".$userName."' and isdelete=0";
			$rs = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($rs);
			if($row){
				$number = strcmp($userEmail,$row['email']);
				if($number == 0){
					$valid = sendEmail($userName,$userEmail);
					if(!empty($valid)){
						$errorTxt.='邮件发送失败!';
						$enErrorTxt='Mail delivery failed!';
					}else{
						$successTxt.='邮件已发送，请注意查收!';
						header("location:returnGetPassInfo.php");
					}
				}else{
					$errorTxt.='输入的邮箱不正确! 如需帮助请(<a href="http://netcube.mobi/?page_id=487" target="_blank">点击这里</a>)联系我们。';
					$enErrorTxt='Your email address does not match your record. Please (<a href="http://netcube.mobi/?page_id=487" target="_blank">click the link</a>) contact us, if you need help.';
				}
			}else {
				$errorTxt.='账号不存在!';
				$enErrorTxt='Account is not there!';
			}
		}else {
			$errorTxt.='邮箱不能为空!';
			$enErrorTxt='Mail can\'t be empty!';
		}
	}else{
		$errorTxt.='用户名不能为空!';
		$enErrorTxt='User name cannot be empty!';
	}
}

/*
 邮件发送
*/
function sendEmail($userName,$userEmail){
	$Name = $userName;
	$userName = base64_encode($userName);
	$time= base64_encode(date('YmdHis'));
	/* recipients */
	$to  ="{$userEmail}";
	if($_SESSION['lang'] =='en'){
	/* subject */
	$subject = "You requested a new NetCube password";
	/* message */
	$message = "
	<html>
	<head>
	<title>Find Password</title>
	</head>
	<body>
	<p>Hi {$Name},</p>
	<table>
	<tr>
	<td>
		Please click on the following link and follow the instruction to reset your password:<br />
		<a href='http://cp.netcube.com.au/api/help/updatePassword.php?userName={$userName}&t={$time}&lang={$_SESSION['lang']}'>http://cp.netcube.com.au/api/help/updatePassword.php?userName={$userName}&t={$time}</a>
	 </td>
	</tr>
	<tr>
	</tr>
	<tr>
	<td><a href='http://NetCube.com.au' target='_blank'>NetCube.com.au</a><br />This is an automatically generated email. Please do not reply.
	</td>
	</tr>
	</table>
	</body>
	</html>
	";
	
	}else{
		/* subject */
		$subject = "诺维网络密码找回";
		/* message */
		$message = "
		<html>
		<head>
		<title>Find Password</title>
		</head>
		<body>
		<p>Hi {$Name},</p>
		<table>
		<tr>
		<td> 
			请点击一下链接来完成密码找回：<br />
			<a href='http://cp.netcube.com.au/api/help/updatePassword.php?userName={$userName}&t={$time}&lang={$_SESSION['lang']}'>http://cp.netcube.com.au/api/help/updatePassword.php?userName={$userName}&t={$time}</a>
		</td>
		</tr>
		<tr>
		</tr>
		<tr>
		<td><a href='http://NetCube.com.au' target='_blank'>NetCube.com.au</a><br />此为系统消息，请勿回复
		</td>
		</tr>
		</table>
		</body>
		</html>
		";
	}
	/* To send HTML mail, you can set the Content-type header. */
	$headers  = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=utf-8";
	/* additional headers
			$headers .= "To: Mary <mary@example.com>, Kelly <kelly@example.com>\r\n";
	$headers .= "From: Birthday Reminder <birthday@example.com>\r\n";
	$headers .= "Cc: birthdayarchive@example.com\r\n";
	$headers .= "Bcc: birthdaycheck@example.com\r\n";
	*/
	/* and now mail it */
	mail($to, $subject, $message, $headers);
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>NetCube</title>
  <meta name="viewport" content="width=device-width,initial-scale=1" />

    <link rel="stylesheet" type="text/css" href="style/reset.css" /> 
    <link rel="stylesheet" type="text/css" href="style/root.css" /> 
    
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/toogle.js"></script>
    
</head>
<body>


	
    <!-- start header -->
    	<div id="header">
         <a href="#"><img src="img/logo.png" width="77" height="24" alt="logo" class="logo" /></a>
         	<a href="index.php" class="button back"><img src="img/back-button.png" width="15" height="16" alt="icon" /></a>
        <div class="clear"></div>
        </div>
    <!-- end header -->
    
    <!-- start page -->
    <div class="page">
    
    		
            <div class="simplebox">
            	<h1 class="titleh"><?php if($_SESSION['lang'] == 'en'){echo 'Lost Password';}else{echo '找回密码';}?></h1>
                <div class="content">
                	
                  <form id="form12" name="form1" method="post" action="getpass.php?method=getPass">
                  
                    <!--提示框-->
                    <?php if(!empty($errorTxt)){ ?>
                    <div class="albox errorbox" >
                      <b><?php  if($_SESSION['lang'] == 'en'){echo 'Input error:';}else{echo '输入错误：';}?></b><?php  
		                      if($_SESSION['lang'] == 'en'){
									echo $enErrorTxt;
								}else{
									echo $errorTxt;
								}
						}else{ ?>   
                       <div class="albox errorbox" style="display:none;">
                      <?php }?>
                      <a href="#" class="close tips" title="close">close</a>
                    </div>
                  		
                    <div class="form-line">
                   	  <label class="st-label"><?php if($_SESSION['lang'] == 'en'){echo 'Username';}else{echo '用户名';}?></label>
                       <input type="text"  id="userName" name="userName" style=" width:80%;"/>
                    </div>
                    
                    <div class="form-line">
                   	  <label class="st-label"><?php if($_SESSION['lang'] == 'en'){echo 'Email Address';}else{echo '电子邮箱';}?></label>
                      <input type="text"  id="userEmail" name="userEmail" style=" width:80%;"/>
                    </div>
                    <!--  
                    <div class="form-line">
                   	  <label class="st-label">验证码</label>
                      <input type="text" name="textfield" id=""  style=" width:15%; float:left;" />
                      <img src="img/yanzhengma.jpg" style="float:left;" />
                      <a href="" style="margin: 25px 0 0 10px; display:block; float:left;">看不清？换一个</a>
                    </div>
                  	-->
                    <div class="clear"></div>
                    

                    
                    <div class="form-line">
                    <input type="submit" name="button" id="button" value="<?php if($_SESSION['lang'] == 'en'){echo 'Submit';}else{echo '提交';}?>" class="submit-button" />
                    </div>

                  </form>
                  
                </div>
      </div>
            <!-- start top button -->
            <div class="topbutton"><a href="#header"><span>Top</span></a></div>
            <!-- end top button -->
            <!-- start footer -->
            <div class="footer">
            © Copyright 2013 . developed by Netcube
            </div>
            <!-- end footer -->
    <div class="clear"></div>
    </div>
    <!-- end page -->
	
    
    
    
    
</body>
</html>
